package com.ttms.dao.impl;

import com.ttms.dao.TicketDao;
import com.ttms.pojo.Ticket;

import java.util.List;

public class TicketDaoImpl extends BaseDao implements TicketDao {
    @Override
    public int addTicket(Ticket ticket) {
        String sql = "insert into ticket(seat_id,sched_id,ticket_price,ticket_status) values(?,?,?,?)";
        return update(sql,ticket.getSeat_id(),ticket.getSched_id(),ticket.getTicket_price(),ticket.getTicket_status());
    }

    @Override
    public int deleteTicketById(Integer id) {
        String sql = "delete from ticket where ticket_id = ?";
        return update(sql,id);
    }

    @Override
    public int update(Ticket ticket) {
        String sql = "update ticket set seat_id=?,sche_id=?,ticket_price=?,ticket_status=?,ticket_locked_time=? where ticket_id = ?";
        return update(sql,ticket.getSeat_id(),ticket.getSched_id(),ticket.getTicket_price(),ticket.getTicket_status(),ticket.getTicket_locked_time(),ticket.getTicket_id());
    }

    @Override
    public int updateTicket_lock(Ticket t) {
        String sql = "update ticket set ticket_status=? , ticket_locked_time = ? where sched_id = ? and seat_id= ? ";
        return update(sql,t.getTicket_status(),t.getTicket_locked_time(),t.getSched_id(),t.getSeat_id());
    }

    @Override
    public Ticket findTicketIdBySeatId(int sched_id, int seat_id) {
        String sql = "select * from ticket where sched_id = ? and seat_id = ? ";

        return queryForOne(Ticket.class,sql ,sched_id,seat_id);
    }

    @Override
    public List<Ticket> findTicketAll() {
        String sql = "select * from ticket limit ?,?";
        return queryForList(Ticket.class, sql);
    }

    @Override
    public Ticket findTicketById(int id) {
        String sql = "select * from ticket where ticket_id = ?";
        return queryForOne(Ticket.class, sql,id);
    }

    @Override
    public int updateTicket(Ticket t) {
        String sql = "update ticket set ticket_status=? , ticket_locked_time = ? where sched_id = ? and seat_id= ? ";
        return update(sql,t.getTicket_status(),t.getTicket_locked_time(),t.getSched_id(),t.getSeat_id());
    }

    @Override
    public Ticket queryTicketBySeatId(Integer id) {
        String sql="select * from ticket where seat_id = ?";
        return queryForOne(Ticket.class, sql,id);
    }

    @Override
    public List<Ticket> queryTickets() {
        return null;
    }

    @Override
    public Integer queryForPageTotalCount() {
        return null;
    }

    @Override
    public Integer queryForTotalCountBySchedule(int sched_id) {
        String sql = "select count(*) from ticket where sched_id = "+ sched_id ;
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }


}
